export function isImage(url) {
  return /\.(jpg|jpeg|png|gif|bmp|webp|svg)$/i.test(url);
}

export function isVideo(url) {
    return /\.(mp4|avi|mov|wmv|flv|mkv|webm|3gp|m4v)$/i.test(url);
}
  
export function getQueryObject(url) {
  url = url == null ? window.location.href : url;
  var search = url.slice(url.indexOf('?') + 1);
  var obj = {};
  var reg = /([^?&=]+)=([^&]*)/g;
  search.replace(reg, (_, $1, $2) => {
    obj[$1] = $2;
  });
  return obj;
}


export const checkImgExists = (imageUrl) => {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.src = imageUrl;
    img.onload = () => {
      console.log(true)
      resolve(true);
    };

    img.onerror = () => {
      resolve(false);
    };
  });
};

export async function checkVideoExists(videoUrl) {
  return new Promise((resolve) => {
    const video = document.createElement("video");
    video.src = videoUrl;
    video.onloadeddata = () => resolve(true); // 视频可加载
    video.onerror = () => resolve(false);     // 视频加载失败
  });
}

export async function checkAudioExists(audioUrl) {
  return new Promise((resolve) => {
    const audio = new Audio();
    audio.src = audioUrl;
    audio.oncanplaythrough = () => resolve(true); // 音频可播放
    audio.onerror = () => resolve(false);        // 音频加载失败
  });
}
  